using Gateway.Common.Models;
using Gateway.IApplication.TaskScheduler;
using Aurora.TaskScheduler.Abstractions.Message;
using Aurora.TaskScheduler.Abstractions.Storage;
using Aurora.TaskScheduler.Extension;

namespace Gateway.Application;

/// <summary>
/// TASK APP
/// </summary>
public class TaskSchedulerApp : ITaskSchedulerApp
{
    private readonly IDataStorage _dataStorage;
    private readonly ITaskHandler _taskHandler;
    /// <summary>
    /// 
    /// </summary>
    /// <param name="dataStorage"></param>
    /// <param name="taskHandler"></param>
    public TaskSchedulerApp( IDataStorage dataStorage, ITaskHandler taskHandler)
    {
        _dataStorage = dataStorage;
        _taskHandler = taskHandler;
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task<string> Add(AppTask input)
    {
        return await _taskHandler.Add(input);
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task<bool> Update(AppTask input)
    {
        return await _taskHandler.Update(input);
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    /// <exception cref="MyException"></exception>
    public async Task<bool> Remove(string id)
    {
        var data=await _dataStorage.GetDetail(id);
        if (data == null)
            throw new MyException("无效任务标识");
        return await _taskHandler.Remove(data);
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    /// <exception cref="MyException"></exception>
    public async Task<bool> Pause(string id)
    {
        var data=await _dataStorage.GetDetail(id);
        if (data == null)
            throw new MyException("无效任务标识");
        return await _taskHandler.Pause(data);
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public  Task<bool> Resume(string id)
    {
        return Task.FromResult(true);
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    /// <exception cref="MyException"></exception>
    public async Task<bool> Run(string id)
    {
        var data=await _dataStorage.GetDetail(id);
        if (data == null)
            throw new MyException("无效任务标识");
        return await _taskHandler.Start(data);
    }
}